import Cookies from 'js-cookie'

/**
 * ! sidebar 侧边栏状态对象 { opened: 是否展开，withoutAnimation：没有动画 }
 * ! device 当前设备
 * ? size 大小，默认值为 medium
 */
const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus')
      ? !!+Cookies.get('sidebarStatus')
      : true,
    withoutAnimation: false,
  },
  device: 'desktop',
  size: Cookies.get('size') || 'medium',
}

const mutations = {
  // ! 切换显示侧边栏
  TOGGLE_SIDEBAR: (state) => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    // 存储侧边栏状态到缓存中，打开为 1，关闭为 0
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  // ! 关闭侧边栏
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  // ! 设置设备属性
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  // ! 设置大小属性，并存储到缓存中
  SET_SIZE: (state, size) => {
    state.size = size
    Cookies.set('size', size)
  },
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  setSize({ commit }, size) {
    commit('SET_SIZE', size)
  },
}

export default { state, mutations, actions }
